import { ref } from "vue";
interface RegisterUser {
  name: string;
  email: string;
  password: string;
  password2: string;
  role: string;
}
export const registerUser = ref<RegisterUser>({
  name: "kaylaee",
  email: "912454064@qq.com",
  password: "123456",
  password2: "123456",
  role: "user",
});

const validatePass2 = (rule: RegisterRules, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerUser.value.password) {
    callback(new Error('两次输入密码不一致!'));
  } else {
    callback();
  }
}

interface RegisterRules {
  name: ({
    message: string;
    required: boolean;
    trigger: string;
  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;
  })[];
  email: {
    type: string;
    message: string;
    required: boolean;
    trigger: string;
  }[];
  password: ({
    message: string;
    required: boolean;
    trigger: string;
  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;
  })[];
  password2: ({
    message: string;
    required: boolean;
    trigger: string;
  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;
  } | {
    validator: (rule: RegisterRules, value: string, callback: any) => void;
    trigger: string;
  })[];
}
export const registerRules = ref<RegisterRules>({
  name: [
    { message: "name can not be null ...",required: true,trigger: "blur",},
    { min: 2, max: 30, message: "长度为2-30之间", trigger: "blur" },
  ],
  email: [
    { type: "email", message: "Email in incorrect ...", required: true, trigger: "blur", },
  ],
  password: [
    { message: "Password could not be empty ...", required: true, trigger: "blur", },
    { min: 6, max: 30, message: "长度为6-30之间", trigger: "blur" }
  ],
  password2: [
    { message: "Password2 could not be empty ...", required: true, trigger: "blur", },
    { min: 6, max: 30, message: "长度为6-30之间", trigger: "blur" },
    { validator: validatePass2, trigger: 'blur' }]
})

